package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.mapper.AccountMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void update(Account entity) {
        // 首先查出当前修改受影响行数,如果行数为0,则没有修改,那就需要回滚事务,抛出运行移除则为回滚
        int rows = accountMapper.updateById(entity);
        if (rows == 0) {
            throw new RuntimeException("乐观锁失败:Account.id" + entity.getId());
        }
    }

    @Override
    public Account getCurrent() {
        return accountMapper.selectById(UserContext.getCurrentUser().getId());
    }
}
